package com.example.mvp_demo2.view.presenter;

import android.os.Handler;

import com.example.mvp_demo2.view.bean.User;
import com.example.mvp_demo2.view.biz.OnLoginListener;
import com.example.mvp_demo2.view.biz.UserBiz;
import com.example.mvp_demo2.view.ui.IUserLoginView;

/**
 * Created by Administrator on 2017/3/26.
 * presenter层,用于处理view层与moudle层的交互
 */

public class UserLoginPresenter {

    private IUserLoginView iUserLoginView;
    private UserBiz userBiz;

    private Handler mHandler = new Handler();

    //构造方法,传入view层的接口
    public UserLoginPresenter(IUserLoginView iUserLoginView) {
        this.iUserLoginView = iUserLoginView;
        //获取moudle层实体对象
        this.userBiz = new UserBiz();
    }

    //登录的业务逻辑
    public void login() {
        iUserLoginView.showLoading();
        userBiz.login(
                iUserLoginView.getUserName(),
                iUserLoginView.getPassWorld(),
                new OnLoginListener() {
                    @Override
                    public void loginSuccess(final User user) {
                        //在主线程中处理登录结果
                        mHandler.post(new Runnable() {
                            @Override
                            public void run() {
                                iUserLoginView.toMainActivity(user);
                                iUserLoginView.hideLoading();
                            }
                        });
                    }

                    @Override
                    public void loginFailed() {
                        //在主线程中处理
                        mHandler.post(new Runnable() {
                            @Override
                            public void run() {
                                iUserLoginView.showFailedError();
                                iUserLoginView.hideLoading();
                            }
                        });
                    }
                });
    }

    //清除按钮操作
    public void clear(){
        iUserLoginView.clearUserName();
        iUserLoginView.ClearPassWorld();
    }


}
